package view;

import controller.GroupChatController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class AddGroupView {
    private final String username;
    private GroupChatController controller = new GroupChatController();

    public AddGroupView(String username) {
        this.username = username;
    }

    public void showUi() {
        JFrame jf = new JFrame("加入群聊");
        jf.setIconImage(Toolkit.getDefaultToolkit().createImage(MainView.class.getResource("qq.jpg")));
        JPanel jp = new JPanel();
        JLabel jLabel = new JLabel("请输入想加入的群聊名称");
        JTextField textField = new JTextField(10);
        jLabel.setFont(new Font("黑体", Font.BOLD, 24));
        JButton bu2 = new JButton("加    入");
        JButton bu3 = new JButton("退    出");
        textField.setFont(new Font("黑体", Font.BOLD, 24));
        jp.setLayout(null);
        jLabel.setBounds(50, 50, 400, 50);
        textField.setBounds(50, 100, 400, 50);
        bu2.setBounds(175, 200, 100, 50);
        bu3.setBounds(175, 300, 100, 50);
        jf.getRootPane().setDefaultButton(bu2);
        jp.add(textField);
        jp.add(jLabel);
        jp.add(bu2);
        jp.add(bu3);
        jf.add(jp);
        jf.setVisible(true);
        jf.setBounds(600, 200, 500, 500);


        bu2.addActionListener(e -> {
            String text = textField.getText();
            boolean b = controller.userAddGroup(username, text);
            if (b) {
                textField.setText("");
                JOptionPane.showMessageDialog(null, "加群成功");
            } else {
                JOptionPane.showMessageDialog(null, "该群不存在或用户已在群聊中");
            }
        });
        bu3.addActionListener(e -> jf.dispose());
        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                jf.dispose();
            }
        });
    }
}